/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

using System;
using System.Collections.Generic;
using System.Text;

namespace FaceCat {
    /// <summary>
    /// 时间层
    /// </summary>
    public class TimeDiv : FCEventCallBack {
        /// <summary>
        /// 创建时间层
        /// </summary>
        public TimeDiv(FCCalendar calendar) {
            m_calendar = calendar;
            onLoad();
        }

        /// <summary>
        /// 小时输入框
        /// </summary>
        public FCSpin m_spinHour;

        /// <summary>
        /// 分钟输入框
        /// </summary>
        public FCSpin m_spinMinute;

        /// <summary>
        /// 秒输入框
        /// </summary>
        public FCSpin m_spinSecond;

        public FCCalendar m_calendar;

        /// <summary>
        /// 获取日历视图
        /// </summary>
        public virtual FCCalendar getCalendar()
        {
            return m_calendar;
        }

        /// <summary>
        /// 设置日历视图
        /// </summary>
        public virtual void setCalendar(FCCalendar value)
        {
            m_calendar = value;
        }

        public int m_height = 40;

        /// <summary>
        /// 获取高度
        /// </summary>
        public virtual int getHeight()
        {
            return m_height;
        }

        /// <summary>
        /// 设置高度
        /// </summary>
        public virtual void setHeight(int value)
        {
            m_height = value;
        }

        /// <summary>
        /// 获取小时
        /// </summary>
        public virtual int getHour()
        {
            if (m_spinHour != null)
            {
                return (int)m_spinHour.getValue();
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// 设置小时
        /// </summary>
        public virtual void setHour(int value)
        {
            if (m_spinHour != null)
            {
                m_spinHour.setValue(value);
            }
        }

        /// <summary>
        /// 获取分钟
        /// </summary>
        public virtual int getMinute()
        {
            if (m_spinMinute != null)
            {
                return (int)m_spinMinute.getValue();
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// 设置分钟
        /// </summary>
        public virtual void setMinute(int value)
        {
            if (m_spinMinute != null)
            {
                m_spinMinute.setValue(value);
            }
        }

        /// <summary>
        /// 获取秒
        /// </summary>
        public virtual int getSecond()
        {
            if (m_spinSecond != null)
            {
                return (int)m_spinSecond.getValue();
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// 设置秒
        /// </summary>
        public virtual void setSecond(int value)
        {
            if (m_spinSecond != null)
            {
                m_spinSecond.setValue(value);
            }
        }

        /// <summary>
        /// 调用事件
        /// </summary>
        /// <param name="eventName"></param>
        /// <param name="sender"></param>
        /// <param name="invoke"></param>
        public void callEvent(String eventName, object sender, object invoke) {
            if(eventName == "onvaluechanged"){
                onSelectedTimeChanged();
            }
        }

        /// <summary>
        /// 销毁方法
        /// </summary>
        public virtual void delete() {
            m_spinHour = null;
            m_spinMinute = null;
            m_spinSecond = null;
        }

        /// <summary>
        /// 获取绘制的背景色
        /// </summary>
        /// <returns>背景色</returns>
        public virtual long getPaintingBackColor() {
            return FCColor.Back;
        }

        /// <summary>
        /// 获取绘制的边线颜色
        /// </summary>
        /// <returns>边线颜色</returns>
        public virtual long getPaintingBorderColor() {
            return FCColor.Border;
        }

        /// <summary>
        /// 获取要绘制的前景色
        /// </summary>
        /// <returns></returns>
        public virtual long getPaintingTextColor() {
            return FCColor.Text;
        }

        /// <summary>
        /// 添加视图方法
        /// </summary>
        public virtual void onLoad() {
            if (m_calendar != null) {
                FCHost host = m_calendar.getNative().getHost();
                if (m_spinHour == null) {
                    m_spinHour = new FCSpin();
                    m_spinHour.setMaximum(23);
                    m_calendar.addView(m_spinHour);
                    m_spinHour.setText("0");
                    m_spinHour.addEvent(this, "onvaluechanged", null);
                }
                if (m_spinMinute == null) {
                    m_spinMinute = new FCSpin();
                    m_spinMinute.setMaximum(59);
                    m_calendar.addView(m_spinMinute);
                    m_spinMinute.setText("0");
                    m_spinMinute.addEvent(this, "onvaluechanged", null);
                }
                if (m_spinSecond == null) {
                    m_spinSecond = new FCSpin();
                    m_spinSecond.setMaximum(59);
                    m_calendar.addView(m_spinSecond);
                    m_spinSecond.setText("0");
                    m_spinSecond.addEvent(this, "onvaluechanged", null);
                }
            }
        }

        /// <summary>
        /// 重绘方法
        /// </summary>
        /// <param name="paint">绘图对象</param>
        /// <param name="clipRect">裁剪区域</param>
        public virtual void onPaint(FCPaint paint, FCRect clipRect) {
            int width = m_calendar.getWidth(), height = m_calendar.getHeight();
            int top = height - m_height;
            FCRect rect = new FCRect(0, height - m_height, width, height);
            paint.fillRect(getPaintingBackColor(), rect);
            if (m_height > 0) {
                long textColor = getPaintingTextColor();
                FCFont font = m_calendar.getFont();
                FCSize tSize = paint.textSize("时", font, -1);
                FCRect tRect = new FCRect();
                tRect.left = width / 3 - tSize.cx;
                tRect.top = top + m_height / 2 - tSize.cy / 2;
                tRect.right = tRect.left + tSize.cx;
                tRect.bottom = tRect.top + tSize.cy;
                paint.drawText("时", textColor, font, tRect, -1);
                tSize = paint.textSize("分", font, -1);
                tRect.left = width * 2 / 3 - tSize.cx;
                tRect.top = top + m_height / 2 - tSize.cy / 2;
                tRect.right = tRect.left + tSize.cx;
                tRect.bottom = tRect.top + tSize.cy;
                paint.drawText("分", textColor, font, tRect, -1);
                tSize = paint.textSize("秒", font, -1);
                tRect.left = width - tSize.cx - 5;
                tRect.top = top + m_height / 2 - tSize.cy / 2;
                tRect.right = tRect.left + tSize.cx;
                tRect.bottom = tRect.top + tSize.cy;
                paint.drawText("秒", textColor, font, tRect, -1);
            }
        }

        /// <summary>
        /// 数值修改事件
        /// </summary>
        public virtual void onSelectedTimeChanged() {
            if (m_calendar != null) {
                m_calendar.onSelectedTimeChanged();
            }
        }

        /// <summary>
        /// 秒表触发方法
        /// </summary>
        public virtual void onTimer() {
        }

        /// <summary>
        /// 更新布局方法
        /// </summary>
        public virtual void update() {
            if (m_height > 0) {
                int width = m_calendar.getWidth(), height = m_calendar.getHeight();
                int top = height - m_height;
                int left = 5;
                if (m_spinHour != null) {
                    m_spinHour.setVisible(true);
                    m_spinHour.setLocation(new FCPoint(left, top + m_height / 2 - m_spinHour.getHeight() / 2));
                    m_spinHour.setWidth((width - 15) / 3 - 20);
                }
                if (m_spinMinute != null) {
                    m_spinMinute.setVisible(true);
                    m_spinMinute.setLocation(new FCPoint(width / 3 + 5, top + m_height / 2 - m_spinMinute.getHeight() / 2));
                    m_spinMinute.setWidth((width - 15) / 3 - 20);
                }
                if (m_spinSecond != null) {
                    m_spinSecond.setVisible(true);
                    m_spinSecond.setLocation(new FCPoint(width * 2 / 3 + 5, top + m_height / 2 - m_spinSecond.getHeight() / 2));
                    m_spinSecond.setWidth((width - 15) / 3 - 20);

                }
            }
            else {
                if (m_spinHour != null) {
                    m_spinHour.setVisible(false);
                }
                if (m_spinMinute != null) {
                    m_spinMinute.setVisible(false);
                }
                if (m_spinSecond != null) {
                    m_spinSecond.setVisible(false);
                }
            }
        }
    }
}
